import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/useAuthStore'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('@/views/LoginView.vue')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/RegisterView.vue')
    },
    {
      path: '/',
      name: 'home',
      redirect: '/dashboard',
      component: () => import('@/views/HomeView.vue'),
      children: [
        {
          path: 'dashboard',
          name: 'dashboard',
          component: () => import('@/views/DashboardView.vue')
        },
        {
          path: 'product',
          name: 'product',
          component: () => import('@/views/product/ProductView.vue'),
          meta: { title: '商品列表', icon: 'product-list' }
        },
        {
          path: 'addProduct',
          name: 'addProduct',
          component: () => import('@/views/product/add'),
          meta: { title: '添加商品', icon: 'product-add' }
        },
        {
          path: 'updateProduct',
          name: 'updateProduct',
          component: () => import('@/views/product/ProductUpdateView.vue'),
          meta: { title: '修改商品', icon: 'product-add' },
          hidden: true
        },
        {
          path: 'productCate',
          name: 'productCate',
          component: () => import('@/views/productCate/index'),
          meta: { title: '商品分类', icon: 'product-cate' }
        },
        {
          path: 'addProductCate',
          name: 'addProductCate',
          component: () => import('@/views/productCate/add'),
          meta: { title: '添加商品分类' },
          hidden: true
        },
        {
          path: 'updateProductCate',
          name: 'updateProductCate',
          component: () => import('@/views/productCate/update'),
          meta: { title: '修改商品分类' },
          hidden: true
        },
        {
          path: 'productAttr',
          name: 'productAttr',
          component: () => import('@/views/productAttr/index'),
          meta: { title: '商品类型', icon: 'product-attr' }
        },
        {
          path: 'productAttrList',
          name: 'productAttrList',
          component: () => import('@/views/productAttr/productAttrList'),
          meta: { title: '商品属性列表' },
          hidden: true
        },
        {
          path: 'addProductAttr',
          name: 'addProductAttr',
          component: () => import('@/views/productAttr/addProductAttr'),
          meta: { title: '添加商品属性' },
          hidden: true
        },
        {
          path: 'updateProductAttr',
          name: 'updateProductAttr',
          component: () => import('@/views/productAttr/updateProductAttr'),
          meta: { title: '修改商品属性' },
          hidden: true
        },
        {
          path: 'brand',
          name: 'brand',
          component: () => import('@/views/brand/index'),
          meta: { title: '品牌管理', icon: 'product-brand' }
        },
        {
          path: 'addBrand',
          name: 'addBrand',
          component: () => import('@/views/brand/add'),
          meta: { title: '添加品牌' },
          hidden: true
        },
        {
          path: 'updateBrand',
          name: 'updateBrand',
          component: () => import('@/views/brand/update'),
          meta: { title: '编辑品牌' },
          hidden: true
        }
      ]
    },
    {
      path: '/404',
      name: '404View',
      component: () => import('@/views/404View.vue'),
      hidden: true
    },
    {
      path: '/:pathMatch(.*)',
      redirect: '/404'
    }
  ]
})

router.beforeEach((to, from, next) => {
  const useAuthStoreData = useAuthStore()
  // 校验登入信息
  if (useAuthStoreData.getAuthenticated || to.fullPath == '/login' || to.fullPath == '/register') {
    next()
  } else {
    router.push({ path: '/login' })
  }
})

export default router
